#include "CorrespondenceGroupingModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.correspondence_grouping_model.json";
}

CorrespondenceGroupingModel::CorrespondenceGroupingModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/correspondence_grouping.html");
}

CorrespondenceGroupingModel& CorrespondenceGroupingModel::get()
{
    static CorrespondenceGroupingModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void CorrespondenceGroupingModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    modelFile_ = json["modelFile"].toString(modelFile_);
    sceneFile_ = json["sceneFile"].toString(sceneFile_);

    showKeypoints_ = json["showKeypoints"].toBool(showKeypoints_);
    showCorrespondences_ = json["showCorrespondences"].toBool(showCorrespondences_);
    useCloudResolution_ = json["useCloudResolution"].toBool(useCloudResolution_);
    useHough_ = json["useHough"].toBool(useHough_);
    modelSamplingSize_ = json["modelSamplingSize"].toDouble(modelSamplingSize_);
    sceneSamplingSize_ = json["sceneSamplingSize"].toDouble(sceneSamplingSize_);
    lrfSupportRadius_ = json["lrfSupportRadius"].toDouble(lrfSupportRadius_);
    shotDescriptorRadius_ = json["shotDescriptorRadius"].toDouble(shotDescriptorRadius_);
    clusteringBinSize_ = json["clusteringBinSize"].toDouble(clusteringBinSize_);
    clusteringThresh_ = json["clusteringThresh"].toDouble(clusteringThresh_);
}

QJsonObject CorrespondenceGroupingModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("modelFile", QJsonValue(modelFile_));
    json.insert("sceneFile", QJsonValue(sceneFile_));
    json.insert("showKeypoints", QJsonValue(showKeypoints_));
    json.insert("showCorrespondences", QJsonValue(showCorrespondences_));
    json.insert("useCloudResolution", QJsonValue(useCloudResolution_));
    json.insert("useHough", QJsonValue(useHough_));
    json.insert("modelSamplingSize", QJsonValue(modelSamplingSize_));
    json.insert("sceneSamplingSize", QJsonValue(sceneSamplingSize_));
    json.insert("lrfSupportRadius", QJsonValue(lrfSupportRadius_));
    json.insert("shotDescriptorRadius", QJsonValue(shotDescriptorRadius_));
    json.insert("clusteringBinSize", QJsonValue(clusteringBinSize_));
    json.insert("clusteringThresh", QJsonValue(clusteringThresh_));

    return json;
}
